package com.cy.controller;


import com.baomidou.mybatisplus.core.metadata.PageList;
import com.cy.bfzcommons.pojo.PassWord;
import com.cy.bfzcommons.pojo.User;
import com.cy.bfzcommons.service.UserService;
import com.cy.bfzcommons.web.util.JsonResult;
import com.cy.bfzcommons.web.util.jwt.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 注意时间日期
     {
     "userName":1234,
     "password":1234,
     "salt":null,
     "email":"1234@qq.com",
     "portrait":"1234.jpg",
     "nickName":1234,
     "birthday":"1989-02-15",
     "phone":13531899984,
     "members":0,
     "address":"1省1市"
     }
     * @param user
     * @return
     */
    //用户新增(注册)
    @PostMapping
    public JsonResult addUser(@ApiParam(name="user",value="用户信息",required=true)@RequestBody User user){
        String s = userService.addUser(user);
        if(StringUtils.hasLength(s)){                                                   //判断是否有值
            return JsonResult.fail(s);
        }
        return JsonResult.ok();
    }

    //查询用户
    @GetMapping
    public JsonResult getUser(@ApiParam(name="pageList",value="分页信息",required=true) PageList pageList){
        System.out.println(userService.getUser(pageList));
        return JsonResult.ok(userService.getUser(pageList));
//        System.out.println(userService.getUser());
//        return JsonResult.success(userService.getUser());
    }

    //通过id查询用户数据
    @GetMapping("/userById/{id}")
    public JsonResult finfUserById(@ApiParam(name="id",value="用户id",required=true)@PathVariable Integer id){
        User user = userService.selectById(id);
        if(user==null){
            return JsonResult.fail();
        }
        return JsonResult.ok(user);
    }

    /**
     格式
     {
     "userId":102,
     "userName":"545454454545",
     "email":"1234@qq.com",
     "portrait":"1234.jpg",
     "nickName":1234,
     "birthday":"1989-02-15",
     "phone":13531899984,
     "address":"1省1市"
     }
     * @param user
     * @return
     */
    //修用户数据修改(不允许修改密码盐值和会员状态)
    @PutMapping
    public JsonResult updataUser(@ApiParam(name="user",value="用户信息",required=true)@RequestBody User user){
        String s = userService.updataUser(user);
        if(s==null){
            return JsonResult.ok();
        }
        return JsonResult.fail(s);
    }

    //修改密码(需要用户输入旧密码和新密码)
    @PutMapping("/updataPassWord")
    public JsonResult updataPassWord(@ApiParam(name="password",value="用户账号密码等信息",required=true)@RequestBody PassWord passWord){
        String s = userService.updataPassWord(passWord);
        if(s==null){
            return JsonResult.ok();
        }
        return JsonResult.fail(s);
    }
    //----------前端------
    //更改状态
    @PutMapping("/members/{userId}/{members}")
    public JsonResult updataMembers(@ApiParam(name="userId",value="用户id",required=true)@PathVariable Integer userId,
                                    @ApiParam(name="members",value="成员",required=true)@PathVariable Integer  members){
        String s = userService.updataMembers(userId, members);
        if(s == null) return JsonResult.ok();
        return JsonResult.fail(s);
    }


    @PutMapping("/updateUser")
    public JsonResult updateUser(@ApiParam(name="user",value="用户信息",required=true) @RequestBody User user){
        String s = userService.updateUserBack(user);
        if(s == null) return JsonResult.ok();
        return JsonResult.fail(s);
    }

    //登录用户登录验证
    @PostMapping("/login")
    public JsonResult Login(@ApiParam(name="user",value="用户信息",required=true) @RequestBody User user){
        String token = userService.Login(user);
        return JsonResult.ok(token);
    }

    //验证token 获取id
    @GetMapping("/checktoken/{token}")
    public JsonResult checktoken(@ApiParam(name="token",value="装数据信息,给前端校验的",required=true) @PathVariable String token){
        return JsonResult.ok(JwtUtil.getuserId(token));
    }








}
